<?php
declare (strict_types=1);

namespace app\common\model;

use addons\V2\admin\model\Product;

class Footprint extends Base
{
    protected $name = 'footprint';

    public function product()
    {
        return $this->hasOne(Product::class, 'id', 'pid')->field('id,title,price,vip_price,integral,icon,sales');
    }

    public static function setFootprint($pid,$uid){
        parent::startTrans();
        try {
            $find=parent::where(['delete_time'=>0,'uid'=>$uid,'pid'=>$pid])->find();

            if($find){
                $find->delete(true);
            }

            $data['uid'] = $uid;
            $data['pid'] = $pid;
            $data['create_time'] =  strtotime(date('Y-m-d',time()));
            parent::create($data);

            parent::commit();
            return ['code' => 1];
        } catch (\Exception $e) {
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
    }


}
